Skip to content

Multithread with CompletableFuture

  • With Java 8 a new feature is available to help developers to code in asynchronus way.

  • From the package java.util.concurrent are now available the class CompletableFuture<T> that implements both interfaces Future<T> and CompletionStage<T>

  • A Future represent the pending result of an asynchronus operation and CompletationStage is a Promise.

  • Promise that the computation will be done in the future

Simple example

public class Foo{
  //...
  public string doSomething(){ return "hey!"}

  public void startFoo(){
    CompletableFuture<String> result = CompletableFuture.supplyAsync(this::doSomething);
  }

}